
package com.marykay.country.love.util;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;


public class StringUtil {
	
	public static String substr(String str,int beginIdx,int length){
	  if(isEmpty(str)){
		  return str;
	  }
	  if(str.length() <= beginIdx){
		  return null;
	  }
	  int endIdx = beginIdx+length;
	  if(endIdx > str.length()){
		  endIdx = str.length();
	  }
	  return str.substring(beginIdx, endIdx);
	}
	
	public static String extract(String regx,String str){
		Pattern p = Pattern.compile(regx);   
		Matcher m = p.matcher(str);       
		if(m.find()) {
			return m.group();
		}
		return null;
	}
	
    /**
     * iso8859-1转化为utf-8编码
     * 
     * @param str
     * @return
     */
    public static String iso2utf(String str){
        String result = StringUtils.stripToEmpty(str);
        try {
            result = new String(result.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
    
    public static String trim(String str){
    	if(str == null){
    		return "";
    	}
    	return str.trim();
    }

    /**
     * @return 将java对象转化为json字符串
     */
    public static String toJson(Object data){
        try {
        	return JSON.toJSONString(data);
//        	return JSONUtil.serialize(data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String toUtf8String(String s){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 0 && c <= 255) {
                sb.append(c);
            } else {
                byte[] b;
                try {
                    b = Character.toString(c).getBytes("utf-8");
                } catch (Exception ex) {
                    System.out.println(ex);
                    b = new byte[0];
                }
                for (int j = 0; j < b.length; j++) {
                    int k = b[j];
                    if (k < 0)
                        k += 256;
                    sb.append("%" + Integer.toHexString(k).toUpperCase());
                }
            }
        }
        return sb.toString();
    }

    public static boolean isEmpty(Object object){
    	if (object == null) {
    		return true;
    	} else {
    		return isEmpty(object.toString());
    	}
    }
    public static boolean isEmpty(String str){
        if (str == null) {
            return true;
        } else {
            return "".equals(str = str.trim()) ? true : false;
        }
    }

    public static Integer toInteger(Object obj){
    	try {
    		return Integer.valueOf(toString(obj));
    	} catch (Exception e) {
    		return Integer.valueOf(0);
    	}
    }
    public static Integer toInteger(String val){
        try {
            return Integer.valueOf(trim(val));
        } catch (Exception e) {
            return Integer.valueOf(0);
        }
    }
    public static Float toFloat(String val){
    	try {
    		return Float.valueOf(trim(val));
    	} catch (Exception e) {
    		return Float.valueOf(0);
    	}
    }

    public static Long toLong(String val){
        try {
            return Long.valueOf(val);
        } catch (Exception e) {
            return Long.valueOf(0);
        }
    }

    public static String toString(Object object){
        return object == null ? "" : object.toString().trim();
    }

    /**
     * 判断2个不为空并且要相等
     * 
     * @param source
     * @param target
     * @return
     */
    public static boolean notNullAndEquls(Object source, Object target){
        if (source == null || target == null)
            return false;
        return source.toString().equals(target.toString());
    }

	public static boolean isEquals(String orginStr,String destStr){
		if(orginStr == null){
			return destStr == null;
		}else{
			return orginStr.equals(destStr);
		}
	}

	public static Long yuanToFen(Double money) {
		
		return Double.valueOf(money*100).longValue();
	}
	
	public static Double fenToYuan(Long money) {
		return money/100.0;
	}

	public static String fill(Object data, int len) {
		String val = data.toString();
		if(val.length() >= len){
			return val;
		}
		StringBuilder sb = new StringBuilder(len);
		for(int i=0;i<len-val.length();i++){
			sb.append("0");
		}
		sb.append(val);
		return sb.toString();
	}
	
	public static int getByteLength(String str){
		if(str == null){
			return 0;
		}
		return str.getBytes().length;
	}
	
	public static String converNullToStr(String str) {
		if (StringUtils.isEmpty(str)) {
			return "";
		} else {
			return str;
		}
	}

}
